package com.example.systemserver.Controller;

import com.example.systemserver.Bean.BookBean;
import com.example.systemserver.Bean.RequestBean;
import com.example.systemserver.Bean.ResultBean;
import com.example.systemserver.Service.BookService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;

    @RequestMapping("/save")
    public ResultBean save(@RequestBody BookBean bookBean) {
        log.info("/book/save,图书信息保存，请求参数:{}", bookBean);
        try {
            bookService.save(bookBean);
            log.info("/book/save,图书信息保存，返回值:null");
            return new ResultBean(200, "保存成功", null);
        } catch (Exception e) {
            log.error("/book/save,图书信息保存失败", e);
            return new ResultBean(500, "保存失败: " + e.getMessage(), null);
        }
    }

    @RequestMapping("/list")
    public ResultBean list(@RequestBody RequestBean requestBean) {
        log.info("/book/list,查询列表，请求参数:{}", requestBean);
        try {
            PageInfo<BookBean> pageInfo = bookService.list(requestBean);
            log.info("/book/list,查询列表，返回值:{}", pageInfo);
            return new ResultBean(200, "查询成功", pageInfo);
        } catch (Exception e) {
            log.error("/book/list,查询列表失败", e);
            return new ResultBean(500, "查询失败: " + e.getMessage(), null);
        }
    }

    @RequestMapping("/remove")
    public ResultBean remove(@RequestBody BookBean bookBean) {
        log.info("/book/remove,删除图书，请求参数:{}", bookBean);
        try {
            boolean flg = bookService.remove(bookBean);
            if (flg) {
                log.info("/book/remove,删除图书成功");
                return new ResultBean(200, "删除成功", null);
            } else {
                log.warn("/book/remove,删除图书失败，未找到对应记录");
                return new ResultBean(500, "删除失败，未找到对应记录", null);
            }
        } catch (Exception e) {
            log.error("/book/remove,删除图书失败", e);
            return new ResultBean(500, "删除失败: " + e.getMessage(), null);
        }
    }

    // 批量删除接口
    @RequestMapping("/removeBatch")
    public ResultBean removeBatch(@RequestBody List<Long> ids) {
        log.info("/book/removeBatch,批量删除图书，请求参数:{}", ids);
        try {
            int count = bookService.removeBatch(ids);
            log.info("/book/removeBatch,批量删除图书成功，删除数量:{}", count);
            return new ResultBean(200, "成功删除 " + count + " 条记录", null);
        } catch (Exception e) {
            log.error("/book/removeBatch,批量删除图书失败", e);
            return new ResultBean(500, "批量删除失败: " + e.getMessage(), null);
        }
    }

    // 全量删除接口
    @RequestMapping("/removeAll")
    public ResultBean removeAll() {
        log.info("/book/removeAll,删除全部图书");
        try {
            int count = bookService.removeAll();
            log.info("/book/removeAll,删除全部图书成功，删除数量:{}", count);
            return new ResultBean(200, "成功删除全部 " + count + " 条记录", null);
        } catch (Exception e) {
            log.error("/book/removeAll,删除全部图书失败", e);
            return new ResultBean(500, "删除全部记录失败: " + e.getMessage(), null);
        }
    }

    @RequestMapping("/info")
    public ResultBean info(@RequestBody BookBean bookBean) {
        log.info("/book/info,查询图书详情，请求参数:{}", bookBean);
        try {
            BookBean info = bookService.info(bookBean);
            if (info != null) {
                log.info("/book/info,查询图书详情成功");
                return new ResultBean(200, "查询成功", info);
            } else {
                log.warn("/book/info,查询图书详情失败，未找到对应记录");
                return new ResultBean(500, "未找到对应记录", null);
            }
        } catch (Exception e) {
            log.error("/book/info,查询图书详情失败", e);
            return new ResultBean(500, "查询失败: " + e.getMessage(), null);
        }
    }
}
